<!DOCTYPE html>
<html>
<head>
	<title>Randomtest</title>
</head>
<body>
<p>Iterations: <input type="number" id="iterations" value="10000"></p>
<p>Seed: <input type="number" id="seed" value="0"></p>
<p><input type="checkbox" id="showText"> Show full text during game</p>
<p><input type="checkbox" id="showCoverage"> Show line coverage statistics after test</p>
<p><input type="checkbox" id="autoScroll"> Automatically scroll to the bottom</p>
<button id="start">Start Randomtest</button>
<script>
var worker = new Worker("randomtest.js");
var messages = [];
worker.onmessage = function(x) {
  var msg = x.data.msg;
  messages.push(msg);
  console.log(msg);
}

setInterval(function() {
  if (messages.length) {
    var frag = document.createDocumentFragment();
    var length = messages.length;
    for (var i = 0; i < length; i++) {
      printBody(messages[i], frag);
      frag.appendChild(document.createElement("br"));
    };
    document.body.appendChild(frag);
    messages = [];
    if (queryArgs.autoScroll) window.scroll(0,document.height);
  }
}, 100);

function printBody(msg, parent) {
    if (msg === null || msg === undefined || msg === "") return;
    if (!parent) parent = document.body;
    if (msg == " ") {
      // IE7 doesn't like innerHTML that's nothing but " "
      parent.appendChild(document.createTextNode(" "));
      return;
    }
    msg = (msg+"").replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;')
      .replace(/\[b\]/g, '<b>')
      .replace(/\[\/b\]/g, '</b>')
      .replace(/\[i\]/g, '<i>')
      .replace(/\[\/i\]/g, '</i>');
    var frag = document.createDocumentFragment();
    temp = document.createElement('div');
    temp.innerHTML = msg;
    while (temp.firstChild) {
        frag.appendChild(temp.firstChild);
    }
    parent.appendChild(frag);

}

var queryArgList = (""+window.location.search).substr(1).split(/&/);
var queryArgs = {};
for (var i = queryArgList.length - 1; i >= 0; i--) {
  var parts = queryArgList[i].split(/=/);
  if ("false" === parts[1]) parts[1] = false;
  queryArgs[parts[0]] = parts[1];
};

if (queryArgs.iterations) document.getElementById("iterations").value = queryArgs.iterations;
if (queryArgs.seed) document.getElementById("seed").value = queryArgs.seed;
if (queryArgs.showText) document.getElementById("showText").checked = queryArgs.showText;
if (queryArgs.showCoverage) document.getElementById("showCoverage").checked = queryArgs.showCoverage;
if (queryArgs.autoScroll) document.getElementById("autoScroll").checked = queryArgs.autoScroll;

document.getElementById("start").onclick = function() {
  iterations = document.getElementById("iterations").value;
  randomSeed = document.getElementById("seed").value*1;
  var showText = document.getElementById("showText").checked;
  var showCoverage = document.getElementById("showCoverage").checked;
  document.body.innerHTML = "";
  worker.postMessage({iterations:iterations, randomSeed:randomSeed, showText:showText, showCoverage:showCoverage});
}
</script>
</body>
</html>